<?php
class Contact extends Eloquent {

	protected $table = 'contacts';

    /**
     * get all contacts
     * @author Thuan Truong
     * @return array
     */
    public function getContactsList () {
        $contacts = Contact::where('is_deleted', '=', 0)->get();
        return $contacts;
    }
    
    /**
     * count all contacts
     * @author Thuan Truong
     */
    public function countAllContacts () {
        $contacts = Contact::where('is_deleted', '=', 0)->count();
        return $contacts;
    }
    
    /**
     * find contact by id
     * @author Thuan Truong
     * @return object
     */
    public function findContactById ($id) {
        $contact = Contact::find($id);
        if (empty($contact)) {
            App::abort(404);
        }
        return $contact;
    }
    
    /**
     * validate form add contact
     * @param input
     * @return array
     * @author Thuan Truong
     */
    public function validateContact($input) {
        $rules = array(
            'name' => 'required|min:3|max:250',
            'email' => 'required|email|max:100',
            'phone' => 'min:8|max:15',
            'address' => 'min:4|max:500',
            'content' => 'required|min:4|max:5000',
            'captcha' => 'required|captcha'
        );
            
        $validator = Validator::make($input, $rules);
        return $validator;
    }
    
    /**
     * store contact in db
     * @param input
     * @return none
     * @author Thuan Truong
     */
    public function store($input) {
        $contact = new Contact;
        $contact->name = $input['name'];
        $contact->phone = $input['phone'];
        $contact->email = $input['email'];
        $contact->address = $input['address'];
        $contact->content = $input['content'];
        $contact->save();
        return $contact;
    }
    
    /**
     * Delete Contact
     * @author Thuan Truong
     * @param id
     */
    public function deleteContact($id) {
        $contact = $this->findContactById($id);
        $contact->is_deleted = 1;
        $contact->update();
    }
}